#include <bits/stdc++.h>

using namespace std;

struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    vector<int> travesal(TreeNode *root)
    {
        vector<int> res;
        if (root == nullptr)
        {
            return res;
        }
        queue<TreeNode *> que;
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode *cur = que.front();
                que.pop();
                res.push_back(cur->val);
                if (cur->left)
                {
                    que.push(cur->left);
                }
                if (cur->right)
                {
                    que.push(cur->right);
                }
            }
        }
        return res;
    }

    // TreeNode *build_tree(vector<int> &nums, int start, int end)
    // {
    //     if (end - start == 0)
    //     {
    //         return nullptr;
    //     }
    //     int index = (end - start) / 2 + start;
    //     int value = nums[index];
    //     TreeNode *root = new TreeNode(value);
    //     int left_start = start;
    //     int left_end = index;
    //     int right_start = index + 1;
    //     int right_end = end;
    //     root->left = build_tree(nums, left_start, left_end);
    //     root->right = build_tree(nums, right_start, right_end);
    //     return root;
    // }
    // TreeNode *sortedArrayToBST(vector<int> &nums)
    // {
    //     return build_tree(nums, 0, nums.size());
    // }

    TreeNode *sortedArrayToBST(vector<int> &nums)
    {
        if (nums.size() == 0)
        {
            return nullptr;
        }
        queue<TreeNode *> node;
        queue<int> left;
        queue<int> right;

        TreeNode *root = new TreeNode(0);
        node.push(root);
        left.push(0);
        right.push(nums.size() - 1);

        while (!node.empty())
        {
            TreeNode *temp = node.front();
            node.pop();
            int left_index = left.front();
            left.pop();
            int right_index = right.front();
            right.pop();
            int mid = left_index + (right_index - left_index) / 2;
            temp->val = nums[mid];

            if (left_index < mid)
            {
                temp->left = new TreeNode(0);
                node.push(temp->left);
                left.push(left_index);
                right.push(mid - 1);
            }

            if (right_index > mid)
            {
                temp->right = new TreeNode(0);
                node.push(temp->right);
                left.push(mid + 1);
                right.push(right_index);
            }
        }

        return root;
    }
};

int main()
{
    std::vector<int> vec{-10, -3, 0, 5, 9};
    Solution sol;
    TreeNode *res = sol.sortedArrayToBST(vec);
    vector<int> vec_show = sol.travesal(res);
    for (auto num : vec_show)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}